import datetime
import smtplib
from email.MIMEText import MIMEText

class mailMessage:
	"""Classe de gestion du texte de mail.
	"""
	
	def __init__(self, debugMode=False,):
		mailMessage._DEBUG = debugMode
		self._message = ""
		self._subject = ""
		self._from = ""
		self._to = ""
		self._smtp = ""
	
	def write(self, message, useTimeStamp=True,):
		"""Ajoute une ligne de texte.
		"""
		newMessage = ""
		if useTimeStamp:
			today = datetime.datetime.today()
			newMessage += "[%02d/%02d/%d %02d:%02d:%02d] "%(today.day, today.month, today.year, today.hour, today.minute, today.second,)
		newMessage += message
		if mailMessage._DEBUG:
			print newMessage
		self._message += newMessage
		self._message += "\r\n"
	
	def writeBR(self,):
		"""Saut de ligne.
		"""
		self._message += "\r\n"
	
	def setSubject(self, subject,):
		self._subject = subject
	
	def setFrom(self, adrFrom,):
		self._from = adrFrom
	
	def setTo(self, adrTo):
		self._to = adrTo
	
	def setSMTP(self, smtp):
		self._smtp = smtp
	
	def getMessage(self,):
		return self._message
	
	# -> Fonctions d'envoi de l'email de test.
	def sendMessage(self, listUsers,):
		"""Envoi de l'email.
		"""
		mail = MIMEText(self.getMessage())
		mail['From'] = self._from
		mail['Subject'] = self._subject
		mail['To'] = ','.join(listUsers)
		s = smtplib.SMTP(self._smtp)
		#s.connect()
		s.sendmail(self._from, listUsers, mail.as_string())
		s.close()
